<?

//Data loading
$title='DAFTAR '.strtoupper(GetParam("source",""));
$perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new GridPDFReport("P","mm","Letter");
$fromDate = GetParam("fromDate","");
$toDate  = GetParam("toDate","");
$pdf->AddPage();
$Result = GridList ();
//if ($Result)
$pdf->GridTable($Result);
$pdf->Output();

function GridList () {
    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    list($day,$month,$year) = explode("/",GetParam("fromDate",""));
    $fromDate = date('Y-m-d',mktime(0,0,0,$month,$day,$year));
    list($day,$month,$year) = explode("/",GetParam("toDate",""));
    $toDate   = date('Y-m-d',mktime(0,0,0,$month,$day,$year));

    if (GetParam("source","")=='Bank') $wherenobkt = "(nobkt like 'TB%' OR nobkt like 'BB%')";
    else $wherenobkt = "(nobkt like 'TK%' OR nobkt like 'BK%')";
    $SQL = "SELECT *, DATE_FORMAT(tglbkt,'%d/%m/%y') as trdate FROM ais_memorial WHERE ".$wherenobkt." AND urut=999 AND tglbkt>='$fromDate' AND tglbkt<='$toDate' ";
    if (GetParam("office",""))
    $SQL .= " AND ktrasl= '".GetParam("office","")."'";

        if (GetParam("skada",""))
    $SQL .= " AND skada= '".GetParam("skada","")."'";

    $SQL .= " ORDER BY nobkt";
    //print $SQL."<br><br>\n\n\n\n";die();
    $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
    return $MyResult;
}

function ReadCOA($no) {
            $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
            $SQL = "SELECT namacc FROM tblcoa WHERE skada='".$no."'";
            $MyResult = $DBConnection->dbc->get_var($SQL);
            return $MyResult;
}

function GetSaldoAwal ($skada,$fdate) {
    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    list($day,$month,$year) = explode('/',$fdate);
    //list($lday,$lmonth,$lyear) = explode('-',date('d-m-Y',mktime(0,0,0,$month,$day-$day,$year)));


  if (GetParam("source","")=='Bank') $wherenobkt = "(nobkt like 'TB%' OR nobkt like 'BB%')";
  else $wherenobkt = "(nobkt like 'TK%' OR nobkt like 'BK%')";

//  $SQL = "SELECT awal FROM tblsaldocb WHERE year='$year' AND skada='".GetParam("skada","")."'";
    // update by chandra (13 feb 09)
    $SQL = "SELECT awal,currency_type FROM tblcoa WHERE ledger='$year' AND skada='".GetParam("skada","")."'";

    $MyResult = $DBConnection->dbc->get_row($SQL);
    $saldoawal = $MyResult->awal;
    // update by chandra (13 feb 09)
    $curr_type = $MyResult->currency_type;

    if($curr_type == 'USD'){
      /*$SQL = "SELECT rate FROM ais_kurssi WHERE curr = 'USD' AND tglbkt <= '".$year."-01-01' ORDER BY tglbkt DESC";
      $execCurr = $DBConnection->dbc->get_row($SQL);
      $saldoawal /= $execCurr->rate;*/
      // update by chandra (18 feb 09)
      if($year > 2008) $SQL = "SELECT jumlah FROM tblsaldodollar_temp WHERE skada = '".GetParam("skada","")."' AND tahun = '$year'";
      else $SQL = "SELECT awal AS jumlah FROM tblsaldocb WHERE skada = '".GetParam("skada","")."' AND year = '2008'";
      $execSaldo = $DBConnection->dbc->get_row($SQL);
      $saldoawal = $execSaldo->jumlah;
    }

//  $SQLCount  = "SELECT SUM(if(dk='D',jumlah,0))-SUM(if(dk='K',jumlah,0)) FROM ais_memorial WHERE tglbkt > '".$year."-01-01' AND tglbkt<'".$year."-".$month."-".$day."' AND ".$wherenobkt." AND skada='".GetParam("skada","")."'";
    // update by chandra (13 feb 09)
    $SQLCount  = "SELECT SUM(if(dk='D',jumlah,0))-SUM(if(dk='K',jumlah,0)) FROM ais_memorial WHERE tglbkt > '".$year."-01-01' AND tglbkt<'".$year."-".$month."-".$day."' AND ".$wherenobkt." AND skada='".GetParam("skada","")."' AND curr = '".$curr_type."'";
    if (GetParam("office",""))
    $SQLCount .= " AND ktrasl= '".GetParam("office","")."'";

    $SQLCount .= " group BY skada";
    //print $SQLCount;
    $subtotalsaldo = $DBConnection->dbc->get_var($SQLCount);
//  print "$subtotalsaldo + $saldoawal"."<p>";
    return ($saldoawal+$subtotalsaldo);
}

// useless
function GetSaldoAwal2 ($skada,$fdate) {
    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    list($day,$month,$year) = explode('/',$fdate);
    list($lday,$lmonth,$lyear) = explode('-',date('d-m-Y',mktime(0,0,0,$month,$day-$day,$year)));


  if (GetParam("source","")=='Bank') $wherenobkt = "(nobkt like 'TB%' OR nobkt like 'BB%')";
  else $wherenobkt = "(nobkt like 'TK%' OR nobkt like 'BK%')";

    $SQL = "SELECT awal, aku$lmonth as lasttotal FROM tblcoa WHERE ledger='$year' AND skada='".GetParam("skada","")."'";
//print $SQL."<p>";
    $MyResult = $DBConnection->dbc->get_row($SQL);
/*  if ($month==1)
    $saldoawal = $MyResult->awal;
    else
    $saldoawal = $MyResult->lasttotal;
*/
    $saldoawal = $MyResult->awal;

//  $SQLCount  = "SELECT SUM(if(dk='D',jumlah,0))-SUM(if(dk='K',jumlah,0)) FROM ais_memorial WHERE tglbkt<'".$year."-".$month."-".$day."' AND tglbkt>='".$year."-".$month."-01' AND ".$wherenobkt." AND skada='".GetParam("skada","")."'";

    $SQLCount  = "SELECT SUM(if(dk='D',jumlah,0))-SUM(if(dk='K',jumlah,0)) FROM ais_memorial WHERE tglbkt > '2008-01-01' AND tglbkt<'".$year."-".$month."-".$day."' AND ".$wherenobkt." AND skada='".GetParam("skada","")."'";

//  print $SQLCount."<p>";
    if (GetParam("office",""))
    $SQLCount .= " AND ktrasl= '".GetParam("office","")."'";

    $SQLCount .= " group BY skada";

    //print $SQLCount;
    $subtotalsaldo = $DBConnection->dbc->get_var($SQLCount);
//  print "$subtotalsaldo + $saldoawal"."<p>";
    return ($subtotalsaldo+($saldoawal/GetParam("forexRate","")));
}

class GridPDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT,$Decimal;
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }
        //$this->SetLeftMargin(5);
        $FDATE = explode('/',GetParam("fromDate",""));
        $TDATE = explode('/',GetParam("toDate",""));
        if (GetParam("forexRate","")<>1) $Decimal=2; else $Decimal=0;
        //$Decimal=2;
        $this->SetFont('arialn','',12);
        $this->Cell(200,5,$perush,0,1,'C',0);
        $this->Cell(200,5,$title,0,1,'C',0);
        $this->Cell(200,5,"PERIOD: ".date('d/m/Y',mktime(0,0,0,$FDATE[1],$FDATE[0],$FDATE[2]))."  -  ".date('d/m/Y',mktime(0,0,0,$TDATE[1],$TDATE[0],$TDATE[2])),0,1,'C',0);
        $this->Ln();
        $this->Cell(100,5,"SKADA: ".GetParam("skada","")." - ".ReadCOA(GetParam("skada","")),0,0,'L',0);
        $this->Cell(100,5,$page." ".$this->PageNo(),0,1,'R',0);
        //$this->SetLeftMargin(5);
//      $this->Line(5,30,205,30);
        $this->Ln();
        //Header
        //Column widths
        $w=array(10,25,15,50,60,20,20);
        //$this->SetFont('arialn','',10);
        $this->Cell($w[0],7,"NO.",1,0,'C',0);
        $this->Cell($w[1],7,"TRANS. NO",1,0,'C',0);
        $this->Cell($w[2],7,"TANGGAL",1,0,'C',0);
        $this->Cell($w[3],7,"KEPADA/DARI",1,0,'C',0);
        $this->Cell($w[4],7,"URAIAN",1,0,'C',0);
        $this->Cell($w[5],7,"DEBET",1,0,'C',0);
        $this->Cell($w[6],7,"CREDIT",1,0,'C',0);
        $this->Ln();
        $this->SetFont('arialn','',10);
    }

    function GridTable($Result) {
        global $Decimal;
        //Column widths
        $w=array(10,25,15,50,60,20,20);
        //Data
        //print GetSaldoAwal (GetParam("skada",""),GetParam("fromDate",""));die();
        $this->Cell($w[0],6,"",'LR',0,'L');
        $this->Cell($w[1],6,"",'LR',0,'L');
        $this->Cell($w[2],6,"",'LR',0,'L');
        $this->Cell($w[3],6,"",'LR',0,'L');
        $this->Cell($w[4],6,"SALDO AWAL:",'LR',0,'R');
        $this->Cell($w[5],6,FormatNumber(GetSaldoAwal (GetParam("skada",""),GetParam("fromDate","")),$Decimal),'LR',0,'R');
        $this->Cell($w[6],6,FormatNumber("0",$Decimal),'LR',0,'R');
        $this->Ln();
        if ($Result) {
        foreach($Result as $row)
        {
            $n++;
            if ($row["dk"]=="D") $row["debet"] = $row["jumlah"];
            else $row["credit"] = $row["jumlah"];
            $totald = $totald + $row["debet"];
            $totalc = $totalc + $row["credit"];

            if (($last<>$row["nobkt"]) && (strlen($nobkt)>0)) {
                $this->Cell($w[0]+$w[1]+$w[2]+$w[3],7,"SUB TOTAL:",1,0,'R',0);
                $this->Cell($w[4],7,FormatNumber($subtotal,$Decimal),1,0,'R',0);
                $this->Ln();
                $subtotal = 0;
            } else {
                $subtotal = $subtotal + $row["jumlah"];
            }
            $this->Cell($w[0],6,"$n.",'LR',0,'R');

            if (($last<>$row["nobkt"]) && (strlen($row["nobkt"])))
                $this->Cell($w[1],6,$row["nobkt"],'LR',0,'L');
            else
                $this->Cell($w[1],6,"",'LR',0,'L');

            $this->Cell($w[2],6,$row["trdate"],'LR',0,'C');
            $this->Cell($w[3],6,strip_tags($row["debnam"]),'LR',0,'L');
            $this->Cell($w[4],6,strip_tags($row["uraian"]),'LR');
            $this->Cell($w[5],6,FormatNumber($row["debet"],$Decimal),'LR',0,'R');
            $this->Cell($w[6],6,FormatNumber($row["credit"],$Decimal),'LR',0,'R');
            $this->Ln();
            $last = $row["nobkt"];
        }
        }
        $this->Cell($w[0],7,'','TL',0,'R',0);
        $this->Cell($w[1]+$w[2]+$w[3]+$w[4],7,"TOTAL MUTASI:",'T',0,'R',0);
        $this->Cell($w[5],7,FormatNumber($totald,$Decimal),1,0,'R',0);
        $this->Cell($w[6],7,FormatNumber($totalc,$Decimal),1,0,'R',0);
        $this->Ln();

            $totald = $totald + GetSaldoAwal (GetParam("skada",""),GetParam("fromDate",""));
            $this->Cell($w[0],7,'','L',0,'R',0);
        $this->Cell($w[1]+$w[2]+$w[3]+$w[4],7,"TOTAL: ",0,0,'R',0);
        $this->Cell($w[5],7,FormatNumber($totald,$Decimal),1,0,'R',0);
        $this->Cell($w[6],7,FormatNumber($totalc,$Decimal),1,0,'R',0);
        $this->Ln();
            $this->Cell($w[0],7,'','L',0,'R',0);
        $this->Cell($w[1]+$w[2]+$w[3]+$w[4],7,"SALDO AKHIR: ",0,0,'R',0);
        $Saldo = $totald-$totalc;
            if ($Saldo<0) {
                  $SaldoD = $Saldo;
                  $SaldoC = 0;
                $this->Cell($w[5],7,FormatNumber($SaldoD,$Decimal),1,0,'R',0);
                $this->Cell($w[6],7,FormatNumber($SaldoC,$Decimal),1,0,'R',0);
        } else {
                  $SaldoD = 0;
                  $SaldoC = $Saldo;
                $this->Cell($w[5],7,FormatNumber($SaldoD,$Decimal),1,0,'R',0);
                $this->Cell($w[6],7,FormatNumber($SaldoC,$Decimal),1,0,'R',0);
        }
        $this->Ln();

        $BalanceD = $totald + $SaldoD;
        $BalanceC = $totalc + $SaldoC;
            /*
            $this->Cell($w[0],7,'','L',0,'R',0);
        $this->Cell($w[1]+$w[2]+$w[3]+$w[4],7,"SALDO AKHIR: ",0,0,'R',0);
        $this->Cell($w[5],7,FormatNumber($total,2),1,0,'R',0);
        $this->Cell($w[6],7,FormatNumber($saldoakhir,2),1,0,'R',0);
        $this->Ln();
*/
        $this->Cell($w[0],7,'','L',0,'R',0);
        $this->Cell($w[1]+$w[2]+$w[3]+$w[4],7,"BALANCE: ",0,0,'R',0);
        $this->Cell($w[5],7,FormatNumber($BalanceD,$Decimal),1,0,'R',0);
        $this->Cell($w[6],7,FormatNumber($BalanceC,$Decimal),1,0,'R',0);
        $this->Ln();
        $this->FinishPage = true;
        //Closure line
        //$this->Cell(array_sum($w),0,'','T');
    }

    function Footer() {
            global $DBConnection;
        //Closure line
        $this->Cell(200,0,'','T');
        $w=array(10,60,60,60,10);
        if ($this->FinishPage) {
            $this->Ln();
            $this->Cell(200,6,"",0,0,'R');
            $this->Ln();$this->Ln();
            $this->Cell($w[0],7,"",0,0,'C',0);
            $this->Cell($w[1],7,"ACKNOWLEDGE",0,0,'C',0);
            $this->Cell($w[2],7,"APPROVED BY",0,0,'C',0);
            $this->Cell($w[3],7,"KASIR",0,0,'C',0);
            $this->Cell($w[4],7,"",0,0,'C',0);
            $this->Ln();$this->Ln();
            $this->Ln();$this->Ln();
            $SQL = "SELECT * FROM $DBName.tblprofile WHERE ktrasl='".GetOfficeID()."'";
            $Result = $DBConnection->dbc->get_row($SQL);

            $this->Cell($w[0],7,"",0,0,'C',0);
            $this->Cell($w[1],7,"( ".$Result->ttdkas1." )",0,0,'C',0);
            $this->Cell($w[2],7,"( ".$Result->ttdkas2." )",0,0,'C',0);
            $this->Cell($w[3],7,"( ".$Result->ttdkas3." )",0,0,'C',0);
            $this->Cell($w[4],7,"",0,0,'C',0);
        }


    }

}


?>
